<?php
function dpa_add_admin_menu() {
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return false;

	add_submenu_page( 'bp-core.php', __( 'Achievements', 'dp-achievements' ), __( 'Achievements', 'dp-achievements' ), 1, 'achievements', 'dpa_admin' );
}
add_action( 'admin_menu', 'dpa_add_admin_menu' );

function dpa_set_achievements_active( $achievements ) {
  global $wpdb, $bp;

	$wpdb->query( "UPDATE {$bp->achievements->table_name} SET active = 0" );

	foreach ( (array) $achievements as $short_name ) {
		$wpdb->query( $wpdb->prepare( "UPDATE {$bp->achievements->table_name} SET active = %d WHERE short_name = %s LIMIT 1", 1, $short_name ) );
	}
}

function dpa_admin() {
	global $bp, $bbpress_live;

	if ( !is_site_admin() )
		return;

	if ( isset( $_POST['submit'] ) && check_admin_referer( 'dp-achievements-nonce' ) ) {

		if ( isset( $_POST['achievements'] ) )
			$status = $_POST['achievements'];
		else
			$status = array();

		dpa_set_achievements_active( $status );
		$updated = true;
	}
?>
<div class="wrap">
	<div class="wrap">
	<div id="icon-options-general" class="icon32"><br /></div>
	<h2><?php _e( 'Achievements', 'dp-achievements' ) ?></h2>

	<br /><p><?php _e( 'This addon brings achievements to BuddyPress, as inspired by World of Warcraft and Xbox Live.', 'dp-achievements' ) ?></p>
	<form id="achievements-list" action="<?php echo site_url() . '/wp-admin/admin.php?page=' . 'achievements' ?>" method="post">

		<?php if ( isset( $updated ) ) : ?><?php echo "<div id='message' class='updated fade'><p>" . __( 'Settings Updated.', 'dp-achievements' ) . "</p></div>" ?><?php endif; ?>
		<h3><?php _e('Achievement status', 'dp-achievements' ) ?></h3>
		<div class="tablenav">
			<div class="alignleft actions">
				<input type="submit" value="<?php _e( 'Save settings', 'dp-achievements' ) ?>" name="submit" class="button-secondary" />
				<?php wp_nonce_field( 'dp-achievements-nonce' ) ?>
			</div>
		</div>
		<table class="widefat" cellspacing="0">
			<thead>
				<tr>
					<th scope="col" class="check-column"><input type="checkbox" /></th>
					<th scope="col"><?php _e( 'Name', 'dp-achievements' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dp-achievements' ) ?></th>
					<th scope="col"><?php _e( 'Category', 'dp-achievements' ) ?></th>
				</tr>
			</thead>

			<tbody id="table-achievements" class="">
			<?php
			$achievements = dpa_get( 'active,short_name,nice_name,description,category' );
			foreach ( (array) $achievements as $a ) {
				$toggled = ( ( $a['active'] ) ? 'checked="checked" ' : ' ' );
				$highlight = ( ($highlight) ? 'alternate' : '' );

				echo '<tr class="' . $highlight . '">';
					echo '<th scope="row" class="check-column"><input type="checkbox" id="' . esc_attr__( $a['short_name'] ) . '" name="achievements[]" value="' . esc_attr__( $a['short_name'] ) . '" ' . $toggled . '/></th>';
					echo '<td class="achievement column-achievement"><strong>' . apply_filters( 'dpa_filter_achievement_nice_name', __( $a['nice_name'], 'dp-achievements' ) ) . '</a></strong></td>';
					echo '<td class="description column-description">' . apply_filters( 'dpa_filter_achievement_description', __( $a['description'], 'dp-achievements' ) ) . '</td>';
					echo '<td class="category column-category">' . apply_filters( 'dpa_filter_achievement_category', __( $a['category'], 'dp-achievements' ) ) . '</td>';
				echo '</tr>';
			} ?>
			</tbody>

			<tfoot>
				<tr>
					<th scope="col" class="check-column"><input type="checkbox" /></th>
					<th scope="col"><?php _e( 'Name', 'dp-achievements' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dp-achievements' ) ?></th>
					<th scope="col"><?php _e( 'Category', 'dp-achievements' ) ?></th>
				</tr>
			</tfoot>
		</table>
		<div class="tablenav">
			<div class="alignleft actions">
				<input type="submit" value="<?php _e( 'Save settings', 'dp-achievements' ) ?>" name="submit" class="button-secondary" />
			</div>
		</div>

		</form>
</div>
<?php
}
?>